#!/bin/sh

killall net_share

if [ -z $1 ]
then
    echo "if first parameter is ap or sta, as follows: "
    echo $"    Usage: $0 <LAN|PPP> ip netmask route" >&2
    echo "if first parameter is wired, as follows: "
    echo $"    Usage: $0 wired ip netmask route ap_ip" >&2
    exit 1
fi

##################set some parma here#########################
if [ ! -d "/var/lib/misc" ];then
    mkdir -p /var/lib/misc
fi

if [ ! -f "/var/lib/misc/udhcpd.leases" ];then
    touch /var/lib/misc/udhcpd.leases
fi

######################iptable common config here.#################
    echo "1" > /proc/sys/net/ipv4/ip_forward
    iptables -X
    iptables -F
    iptables -t nat -X
    iptables -t nat -F

    #iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    #iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
    
###############check ap mode here#############
MODE=$(echo $1 | awk '{print toupper($0)}')
echo "mode: ${MODE}"
############# wifi ap LAN mode##############
if [ $MODE == "LAN" ]
then
    if [ -z $2 ] || [ -z $3 ] || [ -z $4 ]
    then
        IDEV_IP="192.168.2.11"
        IDEV_NETMASK="255.255.255.0"
        IDEV_ROUTE="192.168.2.1"
    else
        IDEV_IP=$2
        IDEV_NETMASK=$3
        IDEV_ROUTE=$4
    fi
    INET=`echo ${IDEV_IP}|cut -d '.' -f 1-3`.0

    echo "LAN IP: ${IDEV_IP}"
    echo "LAN MASK: ${IDEV_NETMASK}"
    echo "LAN ROUTE: ${IDEV_ROUTE}"
    echo "LAN SECTION: ${INET}"

		####################LAN #######################
    RET=`ifconfig -a | grep 'eth0' | awk '{print $1}'`
    if [ "${RET}" = "eth0" ]
    then
        RET=`ifconfig | grep 'eth0' | awk '{print $1}'`
        if [ "${RET}" = "eth0" ]
        then
            echo "${RET} start up already."
        else
            ifconfig eth0 ${IDEV_IP} netmask ${IDEV_NETMASK} up
            sleep 3s
            ifconfig eth0 up
        fi
    fi

		#############wifi ap ip######################
    if [ -z $5 ]
    then
        AP_IP="192.168.10.1"
    else
        AP_IP=$5
    fi
    echo "AP ipaddr: ${AP_IP}"

	#######wlan0 config here##########################
    RET=`/sbin/ifconfig | grep 'wlan0' | awk '{print $1}'`
    if [ -z ${RET} ]
    then
        IDEV="wlan0"
        ifconfig ${IDEV} ${AP_IP} netmask 255.255.255.0 up
    else
        IDEV=${RET}
        ifconfig ${IDEV} ${AP_IP} netmask 255.255.255.0 up
    fi
    
		#############set the iptables here################
    iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    
		################set the route ######################
    echo "NAT INET: ${INET}"
    echo "NAT ROUTE: ${IDEV_ROUTE}"
    echo "route del default ......"
    ip route del default
    sleep 1s
    
    echo "route add default via......"
    ip route add default via ${IDEV_ROUTE} dev eth0
		sleep 1s
		################enable ap here#################
		if test $(pgrep -f "hostapd" | wc -l) -eq 0
    then
        echo "run hostapd ......"
        /usr/bin/hostapd /mnt/mtd/hostapd.conf -B
        sleep 1s
    fi
		################enable dhcp here################
    if test $(pgrep -f "udhcpd" | wc -l) -eq 0
    then
	echo "config udhcpd.conf"
	NET_SEG=${AP_IP%.*}
	echo "net seg:"$NET_SEG
	sed -i "1c start $NET_SEG.2" /etc/udhcpc/udhcpd.conf
	sed -i "2c end $NET_SEG.254" /etc/udhcpc/udhcpd.conf
	sed -i "6c opt router $NET_SEG.1" /etc/udhcpc/udhcpd.conf
        echo "run udhcpd ......"
        /usr/sbin/udhcpd -f /etc/udhcpc/udhcpd.conf &
    fi
        
###################wifi AP ppp mode#####################
elif [ $MODE == "PPP" ]
then
    echo "Enable network interface wlan0 work on $MODE mode." 

    if [ -z $2 ] || [ -z $3 ] || [ -z $4 ]
    then
        IDEV_IP="192.168.0.1"
        IDEV_NETMASK="255.255.255.0"
        IDEV_ROUTE="10.64.64.64"
    else
        IDEV_IP=$2
        IDEV_NETMASK=$3
        IDEV_ROUTE=$4
    fi

    echo "LAN IP: ${IDEV_IP}"
    echo "LAN MASK: ${IDEV_NETMASK}"
    echo "LAN ROUTE: ${IDEV_ROUTE}"

    #############check 4G ppp here###########
    RET=`/sbin/ifconfig | grep 'ppp0'|awk '{print $1}'`
    if [ -z ${RET} ]
    then
        ODEV="ppp0"
        pppd call unicom
    else
        ODEV=${RET}
    fi
		##############
    count=0
    while [ "$count" -lt 18 ]
    do
        var=`ifconfig|grep 'ppp0'`
        if [ -n "$var" ]
        then
            break;
        fi                                    
        echo "wait $count times to ppp0 up..."
        sleep 1s               
        count=`expr $count + 1`
    done
    
    ####################LAN #######################
    RET=`ifconfig -a | grep 'eth0' | awk '{print $1}'`
    if [ "${RET}" = "eth0" ]
    then
        RET=`ifconfig | grep 'eth0' | awk '{print $1}'`
        if [ "${RET}" = "eth0" ]
        then
            echo "${RET} start up already."
        else
            ifconfig eth0 ${IDEV_IP} netmask ${IDEV_NETMASK} up
            sleep 2s
            ifconfig eth0 up
        fi
    fi

		#############wifi ap ip######################
    if [ -z $5 ]
    then
        AP_IP="192.168.10.1"
    else
        AP_IP=$5
    fi
    echo "AP ipaddr: ${AP_IP}"

	#######wlan0 config here##########################
    RET=`/sbin/ifconfig | grep 'wlan0' | awk '{print $1}'`
    if [ -z ${RET} ]
    then
        IDEV="wlan0"
        ifconfig ${IDEV} ${AP_IP} netmask 255.255.255.0 up
    else
        IDEV=${RET}
        ifconfig ${IDEV} ${AP_IP} netmask 255.255.255.0 up
    fi

		#############set the iptables here################
   
    ODEV_ROUTE=`route|grep 'wwan0'|grep -v 'default'|awk '{print $1}'`                             
    echo "New route is ${ODEV_ROUTE}"                                           
    ip route replace default via ${ODEV_ROUTE} 

    iptables -t nat -A POSTROUTING -o ${ODEV} -j MASQUERADE
    iptables -A FORWARD -i ${ODEV} -o ${IDEV} -m state --state RELATED,ESTABLISH -j ACCEPT
    iptables -A FORWARD -i eth0 -o ppp0 -j ACCEPT
    iptables -A FORWARD -i ${IDEV} -o ppp0 -j ACCEPT

    ################enable ap here#################
		if test $(pgrep -f "hostapd" | wc -l) -eq 0
    then
        echo "run hostapd ......"
        /usr/bin/hostapd /mnt/mtd/hostapd.conf -B
        sleep 1s
    fi
		################enable dhcp here################
    if test $(pgrep -f "udhcpd" | wc -l) -eq 0
    then
	echo "config udhcpd.conf"
	NET_SEG=${AP_IP%.*}
	echo "net seg:"$NET_SEG
	sed -i "1c start $NET_SEG.2" /etc/udhcpc/udhcpd.conf
	sed -i "2c end $NET_SEG.254" /etc/udhcpc/udhcpd.conf
	sed -i "6c opt router $NET_SEG.1" /etc/udhcpc/udhcpd.conf

        echo "run udhcpd ......"
        /usr/sbin/udhcpd -f /etc/udhcpc/udhcpd.conf &
    fi
		#################over############
	###################wifi AP WWAN mode#####################
elif [ $MODE == "WWAN" ]
then
    echo "Enable network interface wlan0 work on $MODE mode." 

    if [ -z $2 ] || [ -z $3 ] || [ -z $4 ]
    then
        IDEV_IP="192.168.0.1"
        IDEV_NETMASK="255.255.255.0"
        IDEV_ROUTE="10.64.64.64"
    else
        IDEV_IP=$2
        IDEV_NETMASK=$3
        IDEV_ROUTE=$4
    fi

    echo "LAN IP: ${IDEV_IP}"
    echo "LAN MASK: ${IDEV_NETMASK}"
    echo "LAN ROUTE: ${IDEV_ROUTE}"

    #############check 4G ppp here###########
    RET=`/sbin/ifconfig | grep 'wwan0'|awk '{print $1}'`
    if [ -z ${RET} ]
    then
        ODEV="wwan0"
        quectel-CM -s 3gnet&
    else
        ODEV=${RET}
    fi
		##############
    count=0
    while [ "$count" -lt 18 ]
    do
        var=`ifconfig|grep 'wwan0'`
        if [ -n "$var" ]
        then
            break;
        fi                                    
        echo "wait $count times to wwan0 up..."
        sleep 1s               
        count=`expr $count + 1`
    done
    
    ####################LAN #######################
    RET=`ifconfig -a | grep 'eth0' | awk '{print $1}'`
    if [ "${RET}" = "eth0" ]
    then
        RET=`ifconfig | grep 'eth0' | awk '{print $1}'`
        if [ "${RET}" = "eth0" ]
        then
            echo "${RET} start up already."
        else
            ifconfig eth0 ${IDEV_IP} netmask ${IDEV_NETMASK} up
            sleep 2s
            ifconfig eth0 up
        fi
    fi

		#############wifi ap ip######################
    if [ -z $5 ]
    then
        AP_IP="192.168.10.1"
    else
        AP_IP=$5
    fi
    echo "AP ipaddr: ${AP_IP}"

	#######wlan0 config here##########################
    RET=`/sbin/ifconfig | grep 'wlan0' | awk '{print $1}'`
    if [ -z ${RET} ]
    then
        IDEV="wlan0"
        ifconfig ${IDEV} ${AP_IP} netmask 255.255.255.0 up
    else
        IDEV=${RET}
        ifconfig ${IDEV} ${AP_IP} netmask 255.255.255.0 up
    fi

		#############set the iptables here################
   
    ODEV_ROUTE=`route|grep 'wwan0'|grep -v 'default'|awk '{print $1}'`                             
    echo "New route is ${ODEV_ROUTE}"                                           
    ip route replace default via ${ODEV_ROUTE} 

    iptables -t nat -A POSTROUTING -o ${ODEV} -j MASQUERADE
    iptables -A FORWARD -i ${ODEV} -o ${IDEV} -m state --state RELATED,ESTABLISH -j ACCEPT
    iptables -A FORWARD -i eth0 -o wwan0 -j ACCEPT
    iptables -A FORWARD -i ${IDEV} -o wwan0 -j ACCEPT

    ################enable ap here#################
		if test $(pgrep -f "hostapd" | wc -l) -eq 0
    then
        echo "run hostapd ......"
        /usr/bin/hostapd /mnt/mtd/hostapd.conf -B
        sleep 1s
    fi
		################enable dhcp here################
    if test $(pgrep -f "udhcpd" | wc -l) -eq 0
    then
	echo "config udhcpd.conf"
	NET_SEG=${AP_IP%.*}
	echo "net seg:"$NET_SEG
	sed -i "1c start $NET_SEG.2" /etc/udhcpc/udhcpd.conf
	sed -i "2c end $NET_SEG.254" /etc/udhcpc/udhcpd.conf
	sed -i "6c opt router $NET_SEG.1" /etc/udhcpc/udhcpd.conf

        echo "run udhcpd ......"
        /usr/sbin/udhcpd -f /etc/udhcpc/udhcpd.conf &
    fi
		#################over############	
else
    echo "The input parameter errors, there is no this model."
fi
